dubs - procfs - sysfs - /dev/ - udev

How linux deal with devices :)

/dev

In [19]:
ls /dev/sda* -lFhart
# Major: Driver 
# Minor: Kernel uses to determine exactly which device refered to
brw-rw---- 1 root disk 8, 0 Dec 26 19:25 /dev/sda
brw-rw---- 1 root disk 8, 2 Dec 26 19:25 /dev/sda2
brw-rw---- 1 root disk 8, 5 Dec 26 19:25 /dev/sda5
brw-rw---- 1 root disk 8, 1 Dec 26 19:25 /dev/sda1
In [17]:
ls /dev/dv* -l
lrwxrwxrwx 1 root root 3 Dec 26 19:25 /dev/dvd -> sr0
lrwxrwxrwx 1 root root 3 Dec 26 19:25 /dev/dvdrw -> sr0
In [ ]:
fdisk -l /dev/sda
In [26]:
ls -lF /dev/{random,urandom,zero,null}
crw-rw-rw- 1 root root 1, 3 Dec 26 19:25 /dev/null
crw-rw-rw- 1 root root 1, 8 Dec 26 19:25 /dev/random
crw-rw-rw- 1 root root 1, 9 Dec 26 19:25 /dev/urandom
crw-rw-rw- 1 root root 1, 5 Dec 26 19:25 /dev/zero

ProcFS (Proc FileSystem)

REMEMBER: Evertrhing is a file (sockets, process, devices, etc)

  • Virtual filesystem, things are in memory
  • Contains process information (so other programs can refrence and use)
    • What is the process
    • where is the executable
    • how much memory is using
    • where is the path
    • what memory is available

It was awesome so other developer start using it to save information like cpu, memory, kernel configuration.
Until kernel 2.5 camedout ... (sysfs)

Hint: When you read from /proc, the kernel generates content on the fly. There is no hard drive involved.

In [1]:
ls /proc
1     156   169   27    4081  5386  78         consoles     net
10    1578  17    271   411   5432  79         cpuinfo      pagetypeinfo
1009  1579  1708  28    445   5541  8          crypto       partitions
1012  1583  1711  29    4491  5542  80         devices      sched_debug
1014  1588  1716  3     45    5576  81         diskstats    schedstat
1016  159   1717  30    4521  5631  82         dma          scsi
1020  1591  175   302   46    565   83         driver       self
1023  1592  18    303   461   5678  84         execdomains  slabinfo
1072  1593  180   304   47    5775  85         fb           softirqs
109   1599  182   305   48    5786  86         filesystems  stat
11    16    185   306   4885  5802  864        fs           swaps
110   160   187   31    4891  5808  865        interrupts   sys
1134  161   2     315   4895  5826  866        iomem        sysrq-trigger
1141  1613  20    318   4896  602   87         ioports      sysvipc
1149  1616  2041  32    4897  64    88         irq          thread-self
1174  1619  2045  320   4898  643   89         kallsyms     timer_list
1187  162   2048  322   4922  65    9          kcore        timer_stats
1197  1622  2057  327   4986  68    90         keys         tty
12    1631  2062  329   5     69    91         key-users    uptime
13    1632  2067  33    5072  7     96         kmsg         version
1368  1633  2073  34    5086  70    969        kpagecgroup  version_signature
1374  164   2078  35    5089  71    990        kpagecount   vmallocinfo
1378  1648  2082  36    5097  72    992        kpageflags   vmstat
149   165   21    360   5100  726   994        loadavg      zoneinfo
15    1653  2109  361   5174  73    996        locks
151   1654  22    38    5242  738   acpi       mdstat
1535  166   23    3820  5281  74    asound     meminfo
1537  1663  25    39    5289  745   buddyinfo  misc
154   167   250   3923  5362  75    bus        modules
1544  168   251   398   5371  76    cgroups    mounts
1556  1681  26    40    5384  77    cmdline    mtrr
In [2]:
cat /proc/cpuinfo
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 60
model name	: Intel(R) Core(TM) i3-4000M CPU @ 2.40GHz
stepping	: 3
microcode	: 0x17
cpu MHz		: 2399.906
cache size	: 3072 KB
physical id	: 0
siblings	: 4
core id		: 0
cpu cores	: 2
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm arat pln pts
bugs		:
bogomips	: 4788.97
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:

processor	: 1
vendor_id	: GenuineIntel
cpu family	: 6
model		: 60
model name	: Intel(R) Core(TM) i3-4000M CPU @ 2.40GHz
stepping	: 3
microcode	: 0x17
cpu MHz		: 2400.000
cache size	: 3072 KB
physical id	: 0
siblings	: 4
core id		: 0
cpu cores	: 2
apicid		: 1
initial apicid	: 1
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm arat pln pts
bugs		:
bogomips	: 4788.97
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:

processor	: 2
vendor_id	: GenuineIntel
cpu family	: 6
model		: 60
model name	: Intel(R) Core(TM) i3-4000M CPU @ 2.40GHz
stepping	: 3
microcode	: 0x17
cpu MHz		: 2400.000
cache size	: 3072 KB
physical id	: 0
siblings	: 4
core id		: 1
cpu cores	: 2
apicid		: 2
initial apicid	: 2
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm arat pln pts
bugs		:
bogomips	: 4788.97
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:

processor	: 3
vendor_id	: GenuineIntel
cpu family	: 6
model		: 60
model name	: Intel(R) Core(TM) i3-4000M CPU @ 2.40GHz
stepping	: 3
microcode	: 0x17
cpu MHz		: 2400.000
cache size	: 3072 KB
physical id	: 0
siblings	: 4
core id		: 1
cpu cores	: 2
apicid		: 3
initial apicid	: 3
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm arat pln pts
bugs		:
bogomips	: 4788.97
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:

In [33]:
lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    2
Core(s) per socket:    2
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 60
Model name:            Intel(R) Core(TM) i3-4000M CPU @ 2.40GHz
Stepping:              3
CPU MHz:               2400.093
CPU max MHz:           2400.0000
CPU min MHz:           800.0000
BogoMIPS:              4788.97
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              3072K
NUMA node0 CPU(s):     0-3
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm arat pln pts
In [3]:
cat /proc/meminfo
MemTotal:        3757972 kB
MemFree:         1275708 kB
MemAvailable:    2273804 kB
Buffers:          100916 kB
Cached:          1202024 kB
SwapCached:            0 kB
Active:          1590552 kB
Inactive:         673400 kB
Active(anon):     963476 kB
Inactive(anon):   150016 kB
Active(file):     627076 kB
Inactive(file):   523384 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       7798780 kB
SwapFree:        7798780 kB
Dirty:                76 kB
Writeback:             0 kB
AnonPages:        960912 kB
Mapped:           471636 kB
Shmem:            152488 kB
Slab:             130072 kB
SReclaimable:      99272 kB
SUnreclaim:        30800 kB
KernelStack:        6688 kB
PageTables:        24224 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     9677764 kB
Committed_AS:    3127500 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
HardwareCorrupted:     0 kB
AnonHugePages:    299008 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:      140148 kB
DirectMap2M:     3762176 kB
DirectMap1G:           0 kB
In [4]:
cat /proc/version
Linux version 4.4.0-57-generic (buildd@lgw01-54) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) ) #78-Ubuntu SMP Fri Dec 9 23:50:32 UTC 2016
In [35]:
uname -s
uname -r
Linux
4.4.0-57-generic
In [40]:
cat /proc/sys/kernel/version
#78-Ubuntu SMP Fri Dec 9 23:50:32 UTC 2016
In [5]:
cat /proc/cmdline
BOOT_IMAGE=/vmlinuz-4.4.0-57-generic root=/dev/mapper/Raven--vg-root ro
In [14]:
cat /proc/uptime
#"uptime  idle"
6557.44 24346.54
In [9]:
cd /proc
ls [0-9]*/ -d
# Currently running process
# Htop, Top
1/      10736/  15/    161/   17/    2073/  304/  398/   5362/  72/    84/
10/     10759/  151/   1613/  1708/  2078/  305/  40/    5384/  726/   85/
1009/   10768/  1535/  1616/  1711/  2082/  306/  4081/  5386/  73/    86/
1012/   10792/  1537/  1619/  1716/  21/    31/   411/   5541/  738/   864/
1014/   109/    154/   162/   1717/  2109/  315/  445/   5631/  74/    865/
1016/   11/     1544/  1622/  175/   22/    318/  4491/  565/   745/   866/
1020/   110/    1556/  1631/  18/    23/    32/   45/    5678/  75/    87/
1023/   1134/   156/   1632/  180/   25/    320/  4521/  5802/  76/    88/
10355/  1141/   1578/  1633/  182/   250/   322/  46/    5808/  77/    89/
10377/  1149/   1579/  164/   185/   251/   327/  461/   602/   78/    9/
10490/  1174/   1583/  1648/  187/   26/    329/  47/    64/    7860/  90/
10500/  1187/   1588/  165/   2/     27/    33/   48/    643/   7861/  91/
10530/  1197/   159/   1653/  20/    271/   34/   4891/  65/    7863/  96/
10583/  12/     1591/  1654/  2041/  28/    35/   4895/  6705/  79/    969/
10616/  13/     1592/  166/   2045/  29/    36/   4896/  68/    8/     990/
10620/  1368/   1593/  1663/  2048/  3/     360/  4897/  69/    80/    992/
10651/  1374/   1599/  167/   2057/  30/    361/  4898/  7/     81/    994/
1072/   1378/   16/    168/   2062/  302/   38/   5/     70/    82/    996/
10724/  149/    160/   169/   2067/  303/   39/   5289/  71/    83/
In [15]:
yes &> /dev/null &
[1] 10806
In [16]:
pidof yes
10806
In [17]:
ls -d `pidof yes`
10806
In [18]:
cd 10806
In [19]:
ls
attr             cpuset   limits      net            projid_map  stat
autogroup        cwd      loginuid    ns             root        statm
auxv             environ  map_files   numa_maps      sched       status
cgroup           exe      maps        oom_adj        schedstat   syscall
clear_refs       fd       mem         oom_score      sessionid   task
cmdline          fdinfo   mountinfo   oom_score_adj  setgroups   timers
comm             gid_map  mounts      pagemap        smaps       uid_map
coredump_filter  io       mountstats  personality    stack       wchan
In [20]:
ls -lh cwd
lrwxrwxrwx 1 milad milad 0 Dec 26 23:37 cwd -> /proc
In [21]:
ls -lh exe
lrwxrwxrwx 1 milad milad 0 Dec 26 23:37 exe -> /usr/bin/yes
In [27]:
cat status | head -6
Name:	yes
State:	R (running)
Tgid:	10806
Ngid:	0
Pid:	10806
PPid:	10768
In [ ]:
# A test for cwd in procfs
cd ~
mkdir webserver
dd if=/dev/zero of=file.zip bs=1M count=100
python -m SimpleHTTPServer
cd ~/Downloads
wget --limit-rate=1k http://0.0.0.0:8000/file.zip -O dl.zip
In [ ]:
cd /proc
cd `pidof wget`
ls -l cwd
In [19]:
arp
# makes details more readable (converts hex)
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.1.1              ether   ac:9e:17:52:3f:c4   C                     enp3s0
In [20]:
ip n
192.168.1.1 dev enp3s0 lladdr ac:9e:17:52:3f:c4 STALE
In [2]:
cd /proc/net
cat arp
IP address       HW type     Flags       HW address            Mask     Device
192.168.1.1      0x1         0x2         ac:9e:17:52:3f:c4     *        enp3s0
In [4]:
ls -ld /proc/net
lrwxrwxrwx 1 root root 8 Dec 26 23:51 /proc/net -> self/net
In [5]:
ls -ld /proc/self
lrwxrwxrwx 1 root root 0 Dec 26 19:25 /proc/self -> 10966
In [6]:
ls -ld /proc/self
lrwxrwxrwx 1 root root 0 Dec 26 19:25 /proc/self -> 10967
In [8]:
cd /proc/net
In [17]:
pwd
pwd -P
/proc/net
/proc/10942/net
In [11]:
echo $$
10942
In [13]:
pidof -s bash
10942
In [14]:
type cd
cd is a shell builtin
In [ ]:
help cd

SYSFS

Kernel 2.5 SysFs introduced

  • To contain information about system (dev, etc)
  • Pluged devices
  • Memory space address or memory interrupt
  • Configuration files
  • What brand
  • Mindfulness so: More organized.
  • Procfs still contains old stuff and are valid
In [18]:
ls /sys
block  class  devices   fs          kernel  power
bus    dev    firmware  hypervisor  module
In [28]:
ls /sys/dev/char /sys/dev/block
# Major:Minor
# Driver:Which Device Exactly
/sys/dev/block:
1:0   11:0  1:13  1:2  1:5  1:8    252:1  7:0  7:3  7:6  8:1
1:1   1:11  1:14  1:3  1:6  1:9    252:2  7:1  7:4  7:7  8:2
1:10  1:12  1:15  1:4  1:7  252:0  252:3  7:2  7:5  8:0  8:5

/sys/dev/char:
10:1    10:58   1:3    1:8      248:0  4:20  4:39  4:57  4:75  4:93   7:6
10:130  10:59   13:32  189:0    248:1  4:21  4:4   4:58  4:76  4:94   81:0
10:144  10:60   13:33  189:1    249:0  4:22  4:40  4:59  4:77  4:95   89:0
10:183  10:61   13:63  189:128  249:1  4:23  4:41  4:6   4:78  5:0    89:1
10:184  10:62   13:64  189:129  250:0  4:24  4:42  4:60  4:79  5:1    89:2
10:200  10:63   13:65  189:256  251:0  4:25  4:43  4:61  4:8   5:2    89:3
10:223  108:0   13:66  189:257  29:0   4:26  4:44  4:62  4:80  5:3    89:4
10:227  1:1     13:67  189:384  4:0    4:27  4:45  4:63  4:81  7:0    89:5
10:228  1:11    13:68  1:9      4:1    4:28  4:46  4:64  4:82  7:1    89:6
10:229  116:1   13:69  202:0    4:10   4:29  4:47  4:65  4:83  7:128  89:7
10:231  116:10  13:70  202:1    4:11   4:3   4:48  4:66  4:84  7:129
10:232  116:2   13:71  202:2    4:12   4:30  4:49  4:67  4:85  7:130
10:235  116:3   13:72  202:3    4:13   4:31  4:5   4:68  4:86  7:131
10:236  116:33  13:73  21:0     4:14   4:32  4:50  4:69  4:87  7:132
10:237  116:4   13:74  21:1     4:15   4:33  4:51  4:7   4:88  7:133
10:53   116:5   13:75  226:0    4:16   4:34  4:52  4:70  4:89  7:134
10:54   116:6   13:76  226:128  4:17   4:35  4:53  4:71  4:9   7:2
10:55   116:7   1:4    226:64   4:18   4:36  4:54  4:72  4:90  7:3
10:56   116:8   1:5    246:0    4:19   4:37  4:55  4:73  4:91  7:4
10:57   116:9   1:7    247:0    4:2    4:38  4:56  4:74  4:92  7:5

UDEV

kernel 2.6

  • is a device manager
  • successor of devfsd
  • manages device nodes in the /dev
  • handle all user space events while hardware devices are added

Because of good organization by sysfs udev introduced

  • Populates devices in /dev directory
  • By looking at sysfs

BEFORE UDEV: /dev exists but it was handeling by static links to thinks like: while there first pluged in then unplug but device still is there

In [22]:
whatis udev
udev (7)             - Dynamic device management

DBUS

[inter-process communication]
D-Bus is a message bus system, a simple way for applications to talk to one another.

With udev devices (/dev) are handled dynamicly and in a standard way

dbus:

  • read system informations from /dev
  • tells the program about devices with signals
  • programs does not have to look in:
    • procfs, sysfs, /dev specifically #No hard code, do only your Job

Plug device => dbus signals to DE -> DE Knows about new device connected to system D-Bus helps coordinate process lifecycle;

In [30]:
# dbus-launch (1)      - Utility to start a message bus from a shell script
# .xinitrc
exec dbus-launch --sh-syntax --exit-with-session ssh-agent openbox-session

Lecture notes

License

Creative Commons License

Linux Notes by Milad As (Ravexina) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.


ravexina's gitlab

ravexina's github